function KKB.Save_UserGroup( UserGroup )
	if ( KKB.UserGroups[UserGroup] != nil ) then
		local SQL = "SELECT ID FROM KKB_UserGroups WHERE UserGroup = '"..UserGroup.."'"
		local SpawnSettings = tmysql.escape(util.TableToJSON(KKB.UserGroups[UserGroup].Spawn))
		local PhysgunSettings = tmysql.escape(util.TableToJSON(KKB.UserGroups[UserGroup].Physgun))
		local ToolgunSettings = tmysql.escape(util.TableToJSON(KKB.UserGroups[UserGroup].Toolgun))
		local GravcannonSettings = tmysql.escape(util.TableToJSON(KKB.UserGroups[UserGroup].Gravcannon))
		local DamageSettings = tmysql.escape(util.TableToJSON(KKB.UserGroups[UserGroup].Damage))
		local UseSettings = tmysql.escape(util.TableToJSON(KKB.UserGroups[UserGroup].Use))
		local OtherSettings = tmysql.escape(util.TableToJSON(KKB.UserGroups[UserGroup].Other))
		local DonateSettings = tmysql.escape(util.TableToJSON(KKB.UserGroups[UserGroup].Donate))
		local function Callback( result )
			local SQL2 = nil
			if ( result[1] == nil ) then
				SQL2 = "INSERT INTO KKB_UserGroups ( UserGroup, SpawnSettings, PhysgunSettings, ToolgunSettings, GravcannonSettings, DamageSettings, UseSettings, OtherSettings, DonateSettings ) VALUES ('"..UserGroup.."', '"..SpawnSettings.."', '"..PhysgunSettings.."', '"..ToolgunSettings.."', '"..GravcannonSettings.."', '"..DamageSettings.."', '"..UseSettings.."', '"..OtherSettings.."', '"..DonateSettings.."' )"
			else
				SQL2 = "UPDATE KKB_UserGroups SET SpawnSettings='"..SpawnSettings.."', PhysgunSettings='"..PhysgunSettings.."', ToolgunSettings='"..ToolgunSettings.."', GravcannonSettings='"..GravcannonSettings.."', DamageSettings='"..DamageSettings.."', UseSettings='"..UseSettings.."', OtherSettings='"..OtherSettings.."', DonateSettings='"..DonateSettings.."' WHERE UserGroup = '"..UserGroup.."'"
			end
			if ( SQL2 != nil ) then
				KKB.DBQuery( SQL2, nil )
			end
		end
		KKB.DBQuery( SQL, Callback )
	end
end

function KKB.Save_CustomUser( UniqueID )
	if ( KKB.CustomUsers[UniqueID] != nil ) then
		local SQL = "SELECT ID FROM KKB_CustomUsers WHERE UniqueID = '"..UniqueID.."'"
		local Nick = tmysql.escape(KKB.CustomUsers[UniqueID].Nick)
		local Group = tmysql.escape(KKB.CustomUsers[UniqueID].Group)
		local function Callback( result )
			local SQL2 = nil
			if ( result[1] == nil ) then
				SQL2 = "INSERT INTO KKB_CustomUsers ( UniqueID, Nick, UserGroup ) VALUES ( '"..UniqueID.."', '"..Nick.."', '"..Group.."' )"
			else
				SQL2 = "UPDATE KKB_CustomUsers SET Nick='"..Nick.."', Group='"..Group.."' WHERE UniqueID='"..UniqueID.."'"
			end
			if ( SQL2 != nil ) then
				KKB.DBQuery( SQL2, nil )
			end
		end
		KKB.DBQuery( SQL, Callback )
	end
end

function KKB.Save_Settings()
	local Settings = tmysql.escape(util.TableToJSON(KKB.Settings))
	local SQL = "UPDATE KKB_Settings SET SettingTypes='"..Settings.."' WHERE ID = 1"
	KKB.DBQuery( SQL, nil )
end

function KKB.Save_Damage()
	local Damage = tmysql.escape(util.TableToJSON(KKB.Damage))
	local SQL = "UPDATE KKB_Damage SET DamageTypes='"..Damage.."' WHERE ID = 1"
	KKB.DBQuery( SQL, nil )
end

--WONT DELETE MODELS CORRECTLY
function KKB.Save_Model( Model )
	local SQL = "SELECT ID FROM KKB_Models WHERE ModelName = '"..Model.."'"
	local function Callback( result )
		if ( result[1] == nil ) then
			print("NIL")
		elseif ( result[1] == {} ) then
			print("NIL2")
		elseif ( result[1] == "" ) then
			print("NIL3")
		else
			print("SOME OTHER FUCKING SHIT")
		end
		local SQL2 = nil
		if ( result[1] == nil ) then
			SQL2 = "INSERT INTO KKB_Models ( ModelName ) VALUES ( '"..Model.."' )"
		else
		print(result[1].ID)
			SQL2 = "DELETE FROM KKB_Models WHERE ModelName='"..Model.."'"
		end
		if ( SQL2 != nil ) then
			KKB.DBQuery( SQL2, nil )
		end
	end
	KKB.DBQuery( SQL, Callback )
end

function KKB.Save_Entity( Entity )
	if ( KKB.EntityRestrictions[Entity] != nil )  then
		local SQL = "SELECT ID FROM KKB_Entitys WHERE EntityName='"..Entity.."'"
		local function Callback( result )
			local SQL2 = nil
			local UserGroups = tmysql.escape(util.TableToJSON(KKB.EntityRestrictions[Entity].Groups))
			local Options = tmysql.escape(util.TableToJSON(KKB.EntityRestrictions[Entity].Options))
			if ( result[1] == nil ) then
				SQL2 = "INSERT INTO KKB_Entitys ( EntityName, UserGroups, Options ) VALUES ('"..Entity.."', '"..UserGroups.."', '"..Options.."' )"
			else
				local ID = result[1].ID
				SQL2 = "UPDATE KKB_Entitys SET UserGroups='"..UserGroups.."', Options='"..Options.."' WHERE ID = "..ID
			end
			if ( SQL2 != nil ) then
				KKB.DBQuery( SQL2, nil )
			end
		end
		KKB.DBQuery( SQL, Callback )
	end
end

function KKB.Save_NPC( NPC )
	if ( KKB.NPCRestrictions[NPC] != nil )  then
		local SQL = "SELECT ID FROM KKB_NPCs WHERE NPCName='"..NPC.."'"
		local function Callback( result )
			local SQL2 = nil
			local UserGroups = tmysql.escape(util.TableToJSON(KKB.NPCRestrictions[NPC].Groups))
			local Options = tmysql.escape(util.TableToJSON(KKB.NPCRestrictions[NPC].Options))
			if ( result[1] == nil ) then
				SQL2 = "INSERT INTO KKB_NPCs ( NPCName, UserGroups, Options ) VALUES ('"..NPC.."', '"..UserGroups.."', '"..Options.."' )"
			else
				local ID = result[1].ID
				SQL2 = "UPDATE KKB_NPCs SET UserGroups='"..UserGroups.."', Options='"..Options.."' WHERE ID = "..ID
			end
			if ( SQL2 != nil ) then
				KKB.DBQuery( SQL2, nil )
			end
		end
		KKB.DBQuery( SQL, Callback )
	end
end

function KKB.Save_Vehicle( Vehicle )
	if ( KKB.VehicleRestrictions[Vehicle] != nil )  then
		local SQL = "SELECT ID FROM KKB_Vehicles WHERE VehicleName='"..Vehicle.."'"
		local function Callback( result )
			local SQL2 = nil
			local UserGroups = tmysql.escape(util.TableToJSON(KKB.VehicleRestrictions[Vehicle].Groups))
			local Options = tmysql.escape(util.TableToJSON(KKB.VehicleRestrictions[Vehicle].Options))
			if ( result[1] == nil ) then
				SQL2 = "INSERT INTO KKB_Vehicles ( VehicleName, UserGroups, Options ) VALUES ('"..Vehicle.."', '"..UserGroups.."', '"..Options.."' )"
			else
				local ID = result[1].ID
				SQL2 = "UPDATE KKB_Vehicles SET UserGroups='"..UserGroups.."', Options='"..Options.."' WHERE ID = "..ID
			end
			if ( SQL2 != nil ) then
				KKB.DBQuery( SQL2, nil )
			end
		end
		KKB.DBQuery( SQL, Callback )
	end
end

function KKB.Save_Weapon( Weapon )
	if ( KKB.WeaponRestrictions[Weapon] != nil )  then
		local SQL = "SELECT ID FROM KKB_Weapons WHERE WeaponName='"..Weapon.."'"
		local function Callback( result )
			local SQL2 = nil
			local UserGroups = tmysql.escape(util.TableToJSON(KKB.WeaponRestrictions[Weapon].Groups))
			local Options = tmysql.escape(util.TableToJSON(KKB.WeaponRestrictions[Weapon].Options))
			if ( result[1] == nil ) then
				SQL2 = "INSERT INTO KKB_Weapons ( WeaponName, UserGroups, Options ) VALUES ('"..Weapon.."', '"..UserGroups.."', '"..Options.."' )"
			else
				local ID = result[1].ID
				SQL2 = "UPDATE KKB_Weapons SET UserGroups='"..UserGroups.."', Options='"..Options.."' WHERE ID = "..ID
			end
			if ( SQL2 != nil ) then
				KKB.DBQuery( SQL2, nil )
			end
		end
		KKB.DBQuery( SQL, Callback )
	end
end

function KKB.Save_Tool( Tool )
	if ( KKB.ToolgunRestrictions[Tool] != nil )  then
		local SQL = "SELECT ID FROM KKB_Tools WHERE ToolName='"..Tool.."'"
		local function Callback( result )
		PrintTable(result)
			local SQL2 = nil
			local UserGroups = tmysql.escape(util.TableToJSON(KKB.ToolgunRestrictions[Tool].Groups))
			local Options = tmysql.escape(util.TableToJSON(KKB.ToolgunRestrictions[Tool].Options))
			if ( result[1] == nil ) then
			print("Insert")
				SQL2 = "INSERT INTO KKB_Tools ( ToolName, UserGroups, Options ) VALUES ('"..Tool.."', '"..UserGroups.."', '"..Options.."' )"
			else
				local ID = result[1].ID
				SQL2 = "UPDATE KKB_Tools SET UserGroups='"..UserGroups.."', Options='"..Options.."' WHERE ID = "..ID
			end
			if ( SQL2 != nil ) then
				KKB.DBQuery( SQL2, nil )
			end
		end
		KKB.DBQuery( SQL, Callback )
	end
end